Unity 操作3D模型旋转+放大+移动 | 您所在的位置:网站首页 › unity 物体上下移动 › Unity 操作3D模型旋转+放大+移动 |
一、鼠标控制3D物体移动
1.使用协程
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControlMove : MonoBehaviour
{
Vector3 cubeScreenPos;
Vector3 offset;
void Start()
{
StartCoroutine(OnMouseDown());//在Start方法中调用StartCoroutine(要调用的协程方法)
}
//协程
IEnumerator OnMouseDown()
{
//1. 得到物体的屏幕坐标
cubeScreenPos = Camera.main.WorldToScreenPoint(transform.position);
//2. 计算偏移量
//鼠标的三维坐标
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);
//鼠标三维坐标转为世界坐标
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
offset = transform.position - mousePos;
//3. 物体随着鼠标移动
while (Input.GetMouseButton(0))
{
//目前的鼠标二维坐标转为三维坐标
Vector3 curMousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);
//目前的鼠标三维坐标转为世界坐标
curMousePos = Camera.main.ScreenToWorldPoint(curMousePos);
//物体世界位置
transform.position = curMousePos + offset;
yield return new WaitForFixedUpdate(); //这个很重要,循环执行
}
}
}
2.鼠标左键控制物体移动,鼠标碰到物体,物体颜色改变 ![]() 二、鼠标控制3D物体旋转 1. 控制物体左右旋转,上下旋转 using System.Collections; using System.Collections.Generic; using UnityEngine; public class TransformModel : MonoBehaviour { //上下旋转最大角度限制 public int yMinLimit = -20; public int yMaxLimit = 80; //旋转速度 public float xSpeed = 250.0f;//左右旋转速度 public float ySpeed = 120.0f;//上下旋转速度 //旋转角度 private float x = 0.0f; private float y = 0.0f; void Update() { if (Input.GetMouseButton(1)) { //Input.GetAxis("MouseX")获取鼠标移动的X轴的距离 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMinLimit, yMaxLimit); //欧拉角转化为四元数 Quaternion rotation = Quaternion.Euler(y, x, 0); transform.rotation = rotation; } } //角度范围值限定 static float ClampAngle(float angle, float min, float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp(angle, min, max); } } 2. 控制摄像机以物体为中心旋转 脚本挂载到摄像机上 using System.Collections; using System.Collections.Generic; using UnityEngine; public class TransformModel : MonoBehaviour { public Transform CenObj;//围绕的物体 private Vector3 Rotion_Transform; private new Camera camera; void Start() { camera = GetComponent(); Rotion_Transform = CenObj.position; } void Update() { Ctrl_Cam_Move(); Cam_Ctrl_Rotation(); } //镜头的远离和接近 public void Ctrl_Cam_Move() { if (Input.GetAxis("Mouse ScrollWheel") > 0) { transform.Translate(Vector3.forward * 1f);//速度可调 自行调整 } if (Input.GetAxis("Mouse ScrollWheel") < 0) { transform.Translate(Vector3.forward * -1f);//速度可调 自行调整 } } //摄像机的旋转 public void Cam_Ctrl_Rotation() { var mouse_x = Input.GetAxis("Mouse X");//获取鼠标X轴移动 var mouse_y = -Input.GetAxis("Mouse Y");//获取鼠标Y轴移动 if (Input.GetKey(KeyCode.Mouse1)) { transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 5); transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5); } } }三、鼠标控制3D物体缩放 1. 基于物体本身的Transform的缩放 using System.Collections; using System.Collections.Generic; using UnityEngine; public class TransformModel : MonoBehaviour { //缩放比例限制 public float MinScale = 0.2f; public float MaxScale = 3.0f; //缩放比例 private float scale = 1.0f; void Update() { if (Input.GetAxis("Mouse ScrollWheel") != 0) { scale += Input.GetAxis("Mouse ScrollWheel"); scale = Mathf.Clamp(scale, MinScale, MaxScale); transform.localScale = new Vector3(scale, scale, scale); } } } 2. 基于摄像机的远近的缩放 脚本挂载到摄像机上 using System.Collections; using System.Collections.Generic; using UnityEngine; public class TransformModel : MonoBehaviour { void Update() { if (Input.GetAxis("Mouse ScrollWheel") != 0) { //鼠标滚动滑轮 值就会变化 if (Input.GetAxis("Mouse ScrollWheel") < 0) { //范围值限定 if (Camera.main.fieldOfView 2)//摄像机采用Perspective透视 Camera.main.fieldOfView -= 2; if (Camera.main.orthographicSize >= 1)//摄像机采用Orthograpic正交 Camera.main.orthographicSize -= 0.5F; } } } }
|
CopyRight 2018-2019 实验室设备网 版权所有 |